import da from 'element-ui/src/locale/lang/da'

function param2Obj(url) {
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse(
    '{"' +
    decodeURIComponent(search)
      .replace(/"/g, '\\"')
      .replace(/&/g, '","')
      .replace(/=/g, '":"') +
    '"}'
  )
}

const tokens = {
  admin: {
    token: 'admin-token'
  },
  user1: {
    token: 'user1-token'
  },
  user2: {
    token: 'user2-token'
  },
  user3: {
    token: 'user3-token'
  },
  user4: {
    token: 'user4-token'
  },
  user5: {
    token: 'user5-token'
  },
  user6: {
    token: 'user6-token'
  },
  user7: {
    token: 'user7-token'
  }
}

const users = {
  'admin-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Super Admin',
    company:'一局'
  },
  'user1-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: '一局用户',
    company:'一局'
  },
  'user2-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: '二局用户',
    company:'二局'
  },
  'user3-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: '三局用户',
    company:'三局'
  },
  'user4-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: '四局用户',
    company:'四局'
  },
  'user5-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: '五局用户',
    company:'五局'
  },
  'user6-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: '六局用户',
    company:'六局'
  },
  'user7-token': {
    roles: ['admin'],
    introduction: 'I am an editor',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: '七局用户',
    company:'七局'
  }
}

export default {
  login: res => {
    const { username } = JSON.parse(res.body)
    const data = tokens[username]
    if (data) {
      return {
        code: 200,
        msg:'',
        data
      }
    }
    return {
      code: -1,
      msg: 'Account and password are incorrect.'
    }
  },
  getInfo: res => {
    const { token } = param2Obj(res.url)
    const info = users[token]

    if (info) {
      return {
        code: 200,
        msg:'',
        data: info
      }
    }
    return {
      code: -1,
      msg: 'Login failed, unable to get user details.'
    }
  },
  logout: () => {
    return {
      code: 200,
      msg:'',
      data: 'success'
    }
  }
}
